(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("dynaDiscService", dynaDiscService).
	filter("txnStatus", [ '$filter', '$locale', '$translate','dynaDiscService', function($filter, $locale, $translate, dynaDiscService) {
		return function(optionValue, pattern) {
			return $filter('translate')(dynaDiscService.handTxnStatusLable(optionValue, pattern));
		};
	} ]);
	dynaDiscService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory' , 'storageService', 'dateService'];
	function dynaDiscService($q, loggerFactory, commonService, ajaxServiceFactory, storageService, dateService) {
		var log = loggerFactory.getLogger("dynaDiscService");
		var ajaxServices = ajaxServiceFactory.buildAjaxService({
			getDynaDiscountListOptions: {
				url: 'do/apf/dataList/getDynaDiscountListOptions',
				method: 'get'
			},
			getCurrencyOptions : {
				url : 'do/cda/dataList/currenciesBySiteCode',
				method : 'get'
			}
		});
		
		function getDynaDiscountListOptions() {
			var defer = $q.defer();
			ajaxServices.getDynaDiscountListOptions().then(function(response) {
				defer.resolve(response.data);
			});
			return defer.promise;
		}
		
		function dateDisabled(dateModel){
			var date = dateModel.date, mode = dateModel.mode;
			var bizDate = storageService.getSiteInfo().bizDate;
			if(mode === 'day') {
				return dateService.compareDate(date, bizDate) > 0;
			} else {
				return false;
			}
		};
		
		function getCurrencyOptions() {
			var defer = $q.defer();
			ajaxServices.getCurrencyOptions().then(function(response) {
				defer.resolve(response ? response : {});
			});
			return defer.promise;
		};
		function invoiceRefTypeOptions() {
			return {
				"hasEmptyOption": true,
				"data" : [ {
					"label" : "label.pi.invoicereftype.po",
					"value" : "PO"
				}]
			};
		}
		
		function invoiceTypeOptions() {
			return {
				"hasEmptyOption": true,
				"data" : [ {
					"label" : "label.pi.invoicetype.iv",
					"value" : "IV"
				},{
					"label" : "label.pi.invoicetype.cm",
					"value" : "CM"
				}]
			};
		}
		
		function getSelectionStrategyOptions() {
			return [ {
					"label" : "label.discount.selection.strategy1",
					"value" : "1"
				},{
					"label" : "label.discount.selection.strategy2",
					"value" : "2"
				},{
					"label" : "label.discount.selection.strategy3",
					"value" : "3"
				},{
					"label" : "label.discount.selection.strategy4",
					"value" : "4"
				}];
		};
		
		function getRateTypeOptions() {
			return [ {
					"label" : "label.dynadisc.ratetype.sliding",
					"value" : "SLIDING"
				},{
					"label" : "label.dynadisc.ratetype.static",
					"value" : "STATIC"
				},{
					"label" : "label.dynadisc.ratetype.combo",
					"value" : "COMBO"
				}];
		};
		
		function handTxnStatusLable(optionValue) {
			var options = getTxnStatus();
			var optionLabel = '';
			if(options) {
				angular.forEach(options.data, function(obj, index) {
					if(optionValue == obj.value) {
						optionLabel = obj.label;
					}
				});
			}
			return optionLabel;
		}
		function getTxnStatus() {
			var options = {
				"data" : [ {
					"label" : "",
					"value" : ""
				},{
					"label" : "label.txnstatus.c",
					"value" : "C"
				}, {
					"label" : "label.txnstatus.w",
					"value" : "W"    
				}, {
					"label" : "label.txnstatus.l",
					"value" : "L"    
				}, {
					"label" : "label.txnstatus.j",
					"value" : "J"    
				}, {
					"label" : "label.txnstatus.z",
					"value" : "Z"    
				}, {
					"label" : "label.txnstatus.f",
					"value" : "F"    
				}, {
					"label" : "label.txnstatus.r",
					"value" : "R"    
				}, {
					"label" : "label.txnstatus.x",
					"value" : "X"    
				}, {
					"label" : "label.txnstatus.p",
					"value" : "P"    
				}, {
					"label" : "label.txnstatus.m",
					"value" : "M"    
				} ]
			};
			return options;
		}
		return {
			getDynaDiscountListOptions : getDynaDiscountListOptions,
			invoiceTypeOptions : invoiceTypeOptions,
			getSelectionStrategyOptions : getSelectionStrategyOptions,
			getRateTypeOptions : getRateTypeOptions,
			invoiceRefTypeOptions : invoiceRefTypeOptions,
			getCurrencyOptions : getCurrencyOptions,
			getTxnStatus : getTxnStatus,
			handTxnStatusLable :  handTxnStatusLable,
			dateDisabled : dateDisabled
		};
	}
})(angular);